﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


namespace Web.fanliAdmin.Info
{
    public partial class fanli_promotionAdd : System.Web.UI.Page
    {
        #region DAL
        fanli.DAL.fanli_promotion dal = new fanli.DAL.fanli_promotion();
        #endregion

        #region 属性
        /// <summary>
        /// 正在修改记录的ID
        /// </summary>
        public int Id
        {
            get { return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]); }
            set { ViewState["Id"] = value; }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this);
            if (!IsPostBack)
            {
                FCKeditor1.CustomConfigurationsPath = "/fanliAdmin/fckeditor/CustomConfig/promotionConfig.js";
                BindShop();
                if (Request.QueryString["id"] != null)
                {
                    lblFormTitle.Text = "促销信息修改";
                    int qid = 0;
                    int.TryParse(Request.QueryString["id"], out qid);
                    if (qid != 0)
                    {
                        Id = qid;
                        SetValue();
                    }
                    else
                    {
                        Id = -1;
                        LIB.MessageBox.Show(this, "参数传递有误！");
                    }
                }
                else
                {
                    lblFormTitle.Text = "促销信息添加";
                }
            }
        }

        private void BindShop()
        {
            ddlShop.DataSource = (new fanli.DAL.fanli_shop()).GetDatasetBySql("select id,title from fanli_mall order by id", null);
            ddlShop.DataBind();
            ddlShop.Items.Insert(0, new ListItem("请选择商城", "请选择商城"));
        }

        private void SetValue()
        {
            fanli.Model.fanli_promotion model = dal.GetModel(Id);
            ddlShop.SelectedValue = model.shopid.ToString();
            txtTitle.Text = model.title.ToString();
            FCKeditor1.Value = Server.HtmlDecode(model.content.ToString());
            txtExpiredDate.Text = model.expiredDate.Value.ToShortDateString();
            txtGotoUrl.Text = model.gotoUrl;
        }

        protected void btnSumit_Click(object sender, EventArgs e)
        {
            if (Id == 0)
            {
                Add();
            }
            else
            {
                if (Id == -1)
                {
                    LIB.JsWrite.Alert(this, "id 不存在！");
                    return;
                }
                Modify();
            }
        }

        private void Add()
        {
            #region 获取数据
            int varShopid;	//商城id
            string varTitle;	//标题
            string varContent;	//内容
            DateTime varExpiredDate;	//过期日期
            string varGotoUrl;	//跳转链接
            DateTime varPubDate;	//发布日期
            int varAdminID;	//发布人id
            try
            {
                varShopid = int.Parse(ddlShop.SelectedValue);	//商城id
                varTitle = txtTitle.Text.Trim();	//标题
                varContent = Server.HtmlEncode(FCKeditor1.Value);
                varExpiredDate = DateTime.Parse(txtExpiredDate.Text.Trim());	//过期日期
                varGotoUrl = txtGotoUrl.Text.Trim();	//跳转链接
                varPubDate = DateTime.Now;
                varAdminID = SessionHelp.GetLoginUser(this).id;	//发布人id
            }
            catch
            {
                LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }
            #endregion

            #region 赋值model

            fanli.Model.fanli_promotion model = new fanli.Model.fanli_promotion();
            model.shopid = varShopid;	//商城id
            model.title = varTitle;	//标题
            model.content = varContent;	//内容
            model.expiredDate = varExpiredDate;	//过期日期
            model.gotoUrl = varGotoUrl;	//跳转链接
            model.pubDate = varPubDate;	//发布日期
            model.adminID = varAdminID;	//发布人id
            #endregion

            string where = " title=@title and shopid=@shopid ";
            List<SqlParameter> paramList = new List<SqlParameter>();
            paramList.Add(new SqlParameter("@title", varTitle));
            paramList.Add(new SqlParameter("@shopid", varShopid));
            if (dal.Exists(where, paramList))
            {
                LIB.JsWrite.Alert(this, "促销信息已经存在！");
                return;
            }
            if (dal.Add(model) > 0)
            {
                LIB.MessageBox.Show(this, "促销信息添加成功");
            }
            else
            {
                LIB.MessageBox.Show(this, "促销信息添加失败");
            }
        }

        private void Modify()
        {
            #region 获取数据
            int varShopid;	//商城id
            string varTitle;	//标题
            string varContent;	//内容
            DateTime varExpiredDate;	//过期日期
            string varGotoUrl;	//跳转链接
            try
            {
                varShopid = int.Parse(ddlShop.SelectedValue);	//商城id
                varTitle = txtTitle.Text.Trim();	//标题
                varContent = Server.HtmlEncode(FCKeditor1.Value);
                varExpiredDate = DateTime.Parse(txtExpiredDate.Text.Trim());	//过期日期
                varGotoUrl = txtGotoUrl.Text.Trim();	//跳转链接
            }
            catch
            {
                LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }
            #endregion

            #region 赋值model

            fanli.Model.fanli_promotion model = dal.GetModel(Id);
            model.shopid = varShopid;	//商城id
            model.title = varTitle;	//标题
            model.content = varContent;	//内容
            model.expiredDate = varExpiredDate;	//过期日期
            model.gotoUrl = varGotoUrl;	//跳转链接
            #endregion

            string where = " title=@title and shopid=@shopid and id<>" + Id + "";
            List<SqlParameter> paramList = new List<SqlParameter>();
            paramList.Add(new SqlParameter("@title", varTitle));
            paramList.Add(new SqlParameter("@shopid", varShopid));
            if (dal.Exists(where, paramList))
            {
                LIB.JsWrite.Alert(this, "促销信息已经存在！");
                return;
            }
            if (dal.Update(model) > 0)
            {
                LIB.MessageBox.Show(this, "公告修改成功");
            }
            else
            {
                LIB.MessageBox.Show(this, "公告修改失败");
            }
        }
    }
}